/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.workplan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.workplan.Workplantype;
import com.unis.service.workplan.WorkplantypeService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-03-21
 */
@Controller
@RequestMapping("/workplantype")
public class WorkplantypeController {

    private WorkplantypeService workplantypeService;

    /****************************************
     * Insert a instace of Workplantype
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertWorkplantype(@RequestBody Workplantype workplantype) throws Exception {
        workplantype.setStatmentId("Workplantype.insert");
        String status = "failure";
        workplantypeService.insert(workplantype);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Workplantype
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Workplantype workplantype) throws Exception {
        workplantype.setStatmentId("Workplantype.update");
        String status = "failure";
        workplantypeService.update(workplantype);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Workplantype
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Workplantype workplantype) throws Exception {
        workplantype.setStatmentId("Workplantype.delete");
        String status = "failure";
        workplantypeService.batchDelete(workplantype);
        status = "success";
        return status;
    }
    
    /****************************************
     * 选单
     ****************************************/
    @RequestMapping("/select")
    @ResponseBody
    public List<?> getSelectData() throws Exception {
        List<Object> list = new ArrayList<Object>();

        Workplantype workplantype = new Workplantype();
        workplantype.setStatmentId("Workplantype.queryAsList");
        workplantype.setPaging(false);
        List<BaseModel> workplantypeList = workplantypeService.queryAsList(workplantype);

        for (BaseModel baseModel : workplantypeList) {
            Map<String, Object> map = new HashMap<String, Object>();
            Workplantype pg = (Workplantype) baseModel;
            map.put("value", pg.getTypeid());
            map.put("label", pg.getTypename());
            list.add(map);
        }
        return list;
    }
    /****************************************
     * Query instaces of Workplantype
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Workplantype workplantype = new Workplantype();
        workplantype.setStatmentId("Workplantype.queryAsList");
        BeanUtils.populate(workplantype, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(workplantype, pageMap);
        List<BaseModel> workplantypeList = workplantypeService.queryAsList(workplantype);
        Map<String, Object> renderMap = GridUIMapper.render(workplantype, workplantypeList);
        return renderMap;
    }
    
    /****************************************
     * 查询分类树形结构(一级)
     ****************************************/
    @RequestMapping("/queryFolderLevel")
    @ResponseBody
    public Object queryFolderAsTreeWithOneLayer() throws Exception {
        Workplantype workplantype = new Workplantype();
        workplantype.setStatmentId("Workplantype.queryAsList");
        workplantype.setPaging(false);
        List<BaseModel> workplantypeList = workplantypeService.queryAsList(workplantype);
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("typename", "总分类");
        root.put("children", workplantypeList);
        return root;
    }

    /****************************************
     * Query a instaces of Workplantype
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Workplantype workplantype) throws Exception {
        workplantype.setStatmentId("Workplantype.queryAsObject");
        return workplantypeService.queryAsObject(workplantype);
    }

    @RequestMapping("/page")
    public String forword() {
        return "workplan/workplantypeList";
    }

    @Resource
    public void setWorkplantypeService(WorkplantypeService workplantypeService) {
        this.workplantypeService = workplantypeService;
    }
}
